package com.xhh.nphm.web.controller.system;

import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.system.domain.AfficheComment;
import com.xhh.nphm.system.service.CommentService;
import com.xhh.nphm.system.service.SafficheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @description: 评论
 * @author: 25387
 * @date: 2022/12/28 15:37
 * @version 1.0
 */
@Controller
@RequestMapping("/system/comment")
public class CommentController extends BaseController {
    private final String prefix = "system/user/saf";
    @Autowired
    CommentService commentService;

    @Autowired
    SafficheService safficheService;

    //发表评论、回复评论
    @PostMapping("/addComment")
    public String addComment(@RequestBody AfficheComment afficheComment, ModelMap map){
        afficheComment.setUserId(getSysUser().getUserId());
        afficheComment.setEditTime(new Date());
        if (commentService.addComment(afficheComment)) {
            List<AfficheComment> commentList = safficheService.getAfficheComment(afficheComment.getAfficheId());
            TableDataInfo dataInfo = new TableDataInfo();
            dataInfo.setCode(0);
            dataInfo.setRows(commentList);
            dataInfo.setTotal(commentList.size());
            map.put("comment",dataInfo);
        }
        return prefix +"::commentList";
    }

    //删除评论
    @DeleteMapping ("/deleteComment")
    public String deleteComment(@RequestParam("commentId") Integer commentId, @RequestParam("afficheId")Integer afficheId, ModelMap map){
        if (commentService.deleteComment(commentId)) {
            List<AfficheComment> commentList = safficheService.getAfficheComment(afficheId);
            TableDataInfo dataInfo = new TableDataInfo();
            dataInfo.setCode(0);
            dataInfo.setRows(commentList);
            dataInfo.setTotal(commentList.size());
            map.put("comment",dataInfo);
        }
        return prefix +"::commentList";
    }
}
